package cn.edu.scau.cmi.xiehaizhou.cilent;

import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;

import cn.edu.scau.cmi.xiehaizhou.dao.ScauCmiHibernateSessionFactoryUtil;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.xiehaizhou.dao.StudentDAO;
import cn.edu.scau.cmi.xiehaizhou.dao.TeacherDAO;
import cn.edu.scau.cmi.xiehaizhou.domain.Student;
import cn.edu.scau.cmi.xiehaizhou.domain.Teacher;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Tab;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class HibernateCilentController implements Initializable{

    @FXML
    private TextField txtTeaID2;

    @FXML
    private ComboBox<String> cboChoice2;

    @FXML
    private TextArea txtArea;

    @FXML
    private TextField txtTeaName2;

    @FXML
    private Button btnChoTeaacher;

    @FXML
    private Button btnRun2;

    @FXML
    private Button btnChoStu;

    @FXML
    private TextField txtTeaID;

    @FXML
    private TextField txtStuID2;

    @FXML
    private TextField txtStuID;

    @FXML
    private ComboBox<String> cboChoice;

    @FXML
    private Tab page2;

    @FXML
    private Tab page1;

    @FXML
    private TextArea txtArea2;

    @FXML
    private Button btnRun;

    @FXML
    private TextField txtStuName;
    
    private StudentDAO studentdao = new StudentDAO();
    private TeacherDAO teacherdao = new TeacherDAO();
    
    @Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		// 界面初始化
		cboChoice.getItems().addAll("添加学生","修改学生","删除学生","查看全部学生","查看全部教师","查看指导老师");
		cboChoice2.getItems().addAll("添加教师","修改教师","删除教师","查看全部教师","查看全部学生","查看需要指导的学生");
	}

    @FXML
    void btnRun_Click(ActionEvent event) {
    	switch (cboChoice.getSelectionModel().getSelectedItem()) {
    	case "添加学生":
    		addStudent();
    		break;
    	case "修改学生":
    		modifyStudent();
    		break;
    	case "删除学生":
    		deleteStudent();
    		break;
    	case "查看全部学生":
    		queryStudent();
    		break;
    	case "查看全部教师":
    		queryTeacher();
    		break;
    	case "查看指导老师":
    		queryTutor();
    		break;
    	}
    }

    @FXML
    void btnChoTeaacher_Click(ActionEvent event) {
    	String stuID = txtStuID.getText();
    	String teaID = txtTeaID.getText();
    	if(stuID.length()==0) 
			{txtArea.setText("请输入学生的学号");return;}
    	if(teaID.length()==0) 
			{txtArea.setText("请输入指导老师的ID");return;}
    	Student student = studentdao.findById(Long.parseLong(stuID));
    	if(student == null)
    		{txtArea.setText("不存在该学号的学生");return;}
    	Teacher teacher = teacherdao.findById(Long.parseLong(teaID));
    	if(teacher == null)
    		{txtArea.setText("不存在该教师ID");return;}
    	student.setTeacher(teacher);
    	studentdao.update(student);
    	txtArea.setText("学生"+student.getName()+"选择教师"+teacher.getName()+"为导师");
    }

    @FXML
    void btnChoStu_Click(ActionEvent event) {
    	String stuID = txtStuID2.getText();
    	String teaID = txtTeaID2.getText();
    	if(stuID.length()==0) 
			{txtArea2.setText("请输入学生的学号");return;}
    	if(teaID.length()==0) 
			{txtArea2.setText("请输入指导老师的ID");return;}
    	Student student = studentdao.findById(Long.parseLong(stuID));
    	if(student == null)
    		{txtArea2.setText("不存在该学号的学生");return;}
    	Teacher teacher = teacherdao.findById(Long.parseLong(teaID));
    	if(teacher == null)
    		{txtArea2.setText("不存在该教师ID");return;}
    	if(student.getTeacher() != teacher)
    	{
    		student.setTeacher(teacher);
    		studentdao.update(student);
    		txtArea2.setText("教师"+teacher.getName()+"选择指导学生"+student.getName());
    	}
    	else
    	{
			student.setTeacher(null);
			studentdao.update(student);
			txtArea2.setText("教师"+teacher.getName()+"解除与学生"+student.getName()+"的指导关系");
		}
    }

    @FXML
    void btnRun2_Click(ActionEvent event) {
    	switch (cboChoice2.getSelectionModel().getSelectedItem()) {
    	case "添加教师":
    		addTeacher();
    		break;
    	case "修改教师":
    		modifyTeacher();
    		break;
    	case "删除教师":
    		deleteTeacher();
    		break;
    	case "查看全部学生":
    		queryStudent();
    		break;
    	case "查看全部教师":
    		queryTeacher();
    		break;
    	case "查看需要指导的学生":
    		queryNeededTeaching();
    		break;
    	}
    }
    
    private void addStudent()
    {
    	String stuID = txtStuID.getText(); String stuName = txtStuName.getText();
    	if(stuID.length()==0||stuName.length()==0) 
    		{txtArea.setText("请输入完整的学生信息");return;}
		Student student = new Student();
		student.setId(Long.parseLong(stuID));	
		student.setName(stuName);
		studentdao.save(student);
		txtArea.setText("添加学生" + stuName + "成功");
    }
    private void addTeacher()
    {
    	String teaID = txtTeaID2.getText(); String teaName = txtTeaName2.getText();
    	if(teaID.length()==0||teaName.length()==0) 
    		{txtArea2.setText("请输入完整的教师信息");return;}
		Teacher teacher = new Teacher();
		teacher.setId(Long.parseLong(teaID));
		teacher.setName(teaName);
		teacherdao.save(teacher);
		txtArea2.setText("添加教师" + teaName + "成功");
    }
    private void modifyStudent()
    {
    	String stuID = txtStuID.getText(); String stuName = txtStuName.getText();
    	if(stuID.length()==0||stuName.length()==0) 
			{txtArea.setText("请输入完整的学生信息");return;}
    	Student student = studentdao.findById(Long.parseLong(stuID));
    	if(student == null)
    		{txtArea.setText("不存在该学号的学生");return;}
    	student.setName(stuName);
    	studentdao.update(student);
    	txtArea.setText("学号为"+stuID+"的学生修改成功");
    }
    private void modifyTeacher()
    {
    	String teaID = txtTeaID2.getText(); String teaName = txtTeaName2.getText();
    	if(teaID.length()==0||teaName.length()==0) 
    		{txtArea2.setText("请输入完整的教师信息");return;}
    	Teacher teacher = teacherdao.findById(Long.parseLong(teaID));
    	if(teacher == null)
    		{txtArea2.setText("不存在该教师ID");return;}
    	teacher.setName(teaName);
    	teacherdao.update(teacher);
    	txtArea2.setText("教师ID为"+teaID+"的教师修改成功");
    }
    private void deleteStudent()
    {
    	String stuID = txtStuID.getText();
    	if(stuID.length()==0) 
			{txtArea.setText("请输入学生的学号");return;}
    	Student student = studentdao.findById(Long.parseLong(stuID));
    	if(student == null)
    		{txtArea.setText("不存在该学号的学生");return;}
    	studentdao.delete(student);
    	txtArea.setText("已删除学号为"+stuID+"的学生");
    }
    private void deleteTeacher()
    {
    	String teaID = txtTeaID2.getText();
    	if(teaID.length()==0) 
			{txtArea2.setText("请输入教师ID");return;}
    	Teacher teacher = teacherdao.findById(Long.parseLong(teaID));
    	if(teacher == null)
    		{txtArea2.setText("不存在该教师ID");return;}
    	teacherdao.delete(teacher);
    	txtArea2.setText("已教师ID为"+teaID+"的教师");
    }
    private void queryStudent()
    {
    	txtArea.clear();
    	txtArea2.clear();
		List students = studentdao.findAll();
		Iterator<?> studentIterator = students.iterator();
		while(studentIterator.hasNext()){
			Student student = (Student) studentIterator.next();
			txtArea.appendText("学生ID："+student.getId()+"        "+"学生姓名："+student.getName() + "\n");
			txtArea2.appendText("学生ID："+student.getId()+"        "+"学生姓名："+student.getName() + "\n");
		}
    }
    private void queryTeacher()
    {
    	txtArea.clear();
    	txtArea2.clear();
		List teachers = teacherdao.findAll();
		Iterator<?> teachersIterator = teachers.iterator();
		while(teachersIterator.hasNext()){
			Teacher teacher = (Teacher) teachersIterator.next();
			txtArea.appendText("教师ID："+teacher.getId()+"        "+"教师姓名："+teacher.getName() + "\n");
			txtArea2.appendText("教师ID："+teacher.getId()+"        "+"教师姓名："+teacher.getName() + "\n");
		}
    }
    private void queryTutor()
    {
    	String stuID = txtStuID.getText();
    	if(stuID.length()==0) 
			{txtArea.setText("请输入学生的学号");return;}
    	Student student = studentdao.findById(Long.parseLong(stuID));
    	if(student == null)
			{txtArea.setText("不存在该学号的学生");return;}
    	if(student.getTeacher()==null)
    		txtArea.setText("学生"+student.getName()+"还未选择指导老师");
    	else
    		txtArea.setText("学生"+student.getName()+"的指导老师是："+student.getTeacher().getName());
    }
    private void queryNeededTeaching()
    {
    	txtArea2.clear();
    	String teaID = txtTeaID2.getText();
    	if(teaID.length()==0) 
			{txtArea2.setText("请输入教师ID");return;}
    	Teacher teacher = teacherdao.findById(Long.parseLong(teaID));
    	if(teacher == null)
    		{txtArea2.setText("不存在该教师ID");return;}
    	txtArea2.appendText("教师"+teacher.getName()+"需要指导的学生：\n");
    	Set<Student> studentSet = teacher.getStudents();
    	for (Student student : studentSet) {
			txtArea2.appendText("学生ID："+student.getId()+"        "+"学生姓名："+student.getName() + "\n");
		}
    }
}

